#include "core/util.h"
using namespace std;


namespace core
{
   namespace internal
   {
      String whitespaces = " \t\n\r";
   }
   
   String Util::trim(const String& str)
   {


      String ret;

      u32 start = 0;
      u32 len = str.length();

      // find first non whitespace character
      for (u32 i = 0; i < len; i++)
      {
	 bool ws = false;
	 for (u32 j = 0; j < internal::whitespaces.length(); j++)
	 {
	    if (str[i] == internal::whitespaces[j])
	    {
	       ws = true;
	       break;
	    }
	 }

	 if (!ws)
	 {
	    start = i;
	    break;
	 }
      }

      // find last non whitespace character
      for (u32 i = len; i > start; i--)
      {
	 bool ws = false;
	 for (u32 j = 0; j < internal::whitespaces.length(); j++)
	 {
	    if (str[i] == internal::whitespaces[j])
	    {
	       ws = true;
	       break;
	    }
	 }

	 if (!ws)
	 {
	    len = i - start;
	    break;
	 }
      }
      
      ret = str.substr(start, len);
      return ret;
   };
}

